package com.xtgg.device;

import java.io.FileNotFoundException;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.baby.BabyListActivity;
import com.xtgg.baby.ModifyBabyActivity;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.ToastUtil;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

@SuppressLint("SdCardPath")
public class TextInputActivity extends BaseActivity implements OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_text_input);

		((TextView) findViewById(R.id.center)).setText(getIntent()
				.getStringExtra("title"));
		((TextView) findViewById(R.id.right)).setText("保存");
		((Button) findViewById(R.id.right)).setTextColor(Color
				.parseColor("#EE7C1C"));

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);

		((EditText) findViewById(R.id.content)).setText(getIntent()
				.getStringExtra("content"));
	}

	void changeDeviceName()
	{
		
	}
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;

		case R.id.right:
			if(getIntent().getStringExtra("title").equals("设备昵称"))
			{
				sendHttpRequest();
			}
			else
			{
				Intent data = new Intent();
				data.putExtra("content", ((EditText) findViewById(R.id.content))
						.getText().toString());
				setResult(RESULT_OK, data);
				finish();
			}
			break;
		}
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("state").equals("1")) {
					Intent data = new Intent();
					data.putExtra("content", ((EditText) findViewById(R.id.content))
							.getText().toString());
					setResult(RESULT_OK, data);
					finish();
				} 
				else if (ruser.getString("state").equals("2"))
				{
					showExitDialog();
				} 
				else {
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
		};

		RequestParams params = new RequestParams();
		params.put("act", "updateDeviceInfo");
		params.put("nickname", ((EditText) findViewById(R.id.content))
				.getText().toString());
		params.put("deviceid", getIntent().getStringExtra("deviceid"));
		MgqRestClient.post(Constants.SETTING, params, loginHandler);
	}
}
